package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import pers.cj.pdos.constant.OrderType;
import pers.cj.pdos.entity.*;
import pers.cj.pdos.mapper.*;
import pers.cj.pdos.service.ISaleOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.SaleOrderVo;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class SaleOrderServiceImpl extends ServiceImpl<SaleOrderMapper, SaleOrder> implements ISaleOrderService {
    @Autowired
    SaleOrderMapper saleOrderMapper;
    @Autowired
    SaleOrderGoodsMapper saleOrderGoodsMapper;
    @Autowired
    SaleOrderPacketMapper saleOrderPacketMapper;
    @Autowired
    SaleOrderPacketGoodsMapper saleOrderPacketGoodsMapper;

    @Override
    public boolean checkNo(String no, String companyId) {
        return saleOrderMapper.selectCount(new QueryWrapper<SaleOrder>().eq("company_id",companyId).eq("no",no)) > 0;
    }

    @Override
    public void insert(SaleOrder saleOrder, List<SaleOrderGoods> saleOrderGoodsList,
                       List<SaleOrderPacket> saleOrderPacketList,
                       List<SaleOrderPacketGoods> saleOrderPacketGoodsList) {
        saleOrderMapper.insert(saleOrder);

        if (!Utils.isEmpty(saleOrderGoodsList)) {
            saleOrderGoodsMapper.insertBatch(saleOrderGoodsList);
        }
        if (!Utils.isEmpty(saleOrderPacketList)) {
            saleOrderPacketMapper.insertBatch(saleOrderPacketList);
        }
        if (!Utils.isEmpty(saleOrderPacketGoodsList)) {
            saleOrderPacketGoodsMapper.insertBatch(saleOrderPacketGoodsList);
        }
    }

    @Override
    public void update(SaleOrder saleOrder, List<SaleOrderGoods> saleOrderGoodsList,
                       List<SaleOrderPacket> saleOrderPacketList,
                       List<SaleOrderPacketGoods> saleOrderPacketGoodsList) {
        saleOrderMapper.updateById(saleOrder);
        saleOrderGoodsMapper.delete(new QueryWrapper<SaleOrderGoods>().eq("sale_order_id",saleOrder.getId()));
        saleOrderPacketMapper.delete(new QueryWrapper<SaleOrderPacket>().eq("sale_order_id",saleOrder.getId()));
        saleOrderPacketGoodsMapper.delete(new QueryWrapper<SaleOrderPacketGoods>().eq("sale_order_id",saleOrder.getId()));

        if (!Utils.isEmpty(saleOrderGoodsList)) {
            saleOrderGoodsMapper.insertBatch(saleOrderGoodsList);
        }
        if (!Utils.isEmpty(saleOrderPacketList)) {
            saleOrderPacketMapper.insertBatch(saleOrderPacketList);
        }
        if (!Utils.isEmpty(saleOrderPacketGoodsList)) {
            saleOrderPacketGoodsMapper.insertBatch(saleOrderPacketGoodsList);
        }
    }


    @Override
    public List<SaleOrderVo> listVo(String companyId,
                                    Integer page,
                                    Integer limit,
                                    Boolean close,
                                    String search,
                                    String no,
                                    String goodsName,
                                    String remark,
                                    String custom,
                                    Date deliveryDate1,
                                    Date deliveryDate2,
                                    Date makeTime1,
                                    Date makeTime2,
                                    String storeId,
                                    String userStoreId,
                                    String userId,
                                    Date date1,
                                    Date date2,
                                    String makeUserId,
                                    Integer status) {
        return saleOrderMapper.listVo(companyId,
                Utils.genPage(page,limit),
                limit,
                close,
                search,
                no,
                goodsName,
                remark,
                custom,
                deliveryDate1,
                deliveryDate2,
                makeTime1,
                makeTime2,
                storeId,
                userStoreId,
                userId,
                date1,
                date2,
                makeUserId,
                status);
    }

    @Override
    public List<SaleOrderVo> listVoByIds(List<String> ids) {
        return saleOrderMapper.listVoByIds(ids);
    }

    @Override
    public long count(String companyId,
                      Boolean close,
                      String search,
                      String no,
                      String goodsName,
                      String remark,
                      String custom,
                      Date deliveryDate1,
                      Date deliveryDate2,
                      Date makeTime1,
                      Date makeTime2,
                      String storeId,
                      String userStoreId,
                      String userId,
                      Date date1,
                      Date date2,
                      String makeUserId,
                      Integer status) {
        return saleOrderMapper.count(companyId,
                close,
                search,
                no,
                goodsName,
                remark,
                custom,
                deliveryDate1,
                deliveryDate2,
                makeTime1,
                makeTime2,
                storeId,
                userStoreId,
                userId,
                date1,
                date2,
                makeUserId,
                status);
    }

    @Override
    public SaleOrderVo getVoById(String id) {
        return saleOrderMapper.getVoById(id);
    }

    @Override
    public void batchClose(List<SaleOrder> list) {
        if (Utils.isEmpty(list)){
            return;
        }
        saleOrderMapper.batchClose(list);
    }

    @Override
    public void batchVerify(String verifyUserId, List<SaleOrder> list) {
        if (Utils.isEmpty(list)){
            return;
        }
        saleOrderMapper.batchVerify(verifyUserId, list);
    }

    @Override
    public void batchBackVerify(List<SaleOrder> list) {
        if (Utils.isEmpty(list)){
            return;
        }
        saleOrderMapper.batchBackVerify(list);

    }

    @Override
    public boolean checkClose(SaleOrder saleOrder) {
        return saleOrderMapper.selectCount(new QueryWrapper<SaleOrder>().eq("id",saleOrder.getId()).eq("close",true))  > 0;
    }
}
